﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Net;
using System.Threading;
using System.Net.Sockets;
using System.IO;

namespace Chaos.network
{
    public class detectionclient
    {

        private Thread m_thread;
        udpconnection m_sendconnection;
        udpconnection m_receiveconnection;
        private bool m_running;

        public static int Port { get { return 48929; } }

        public detectionclient()
        {
            m_thread = new Thread(new ThreadStart(threadmain));
            m_thread.IsBackground = true;
            m_thread.Name = "detectionclient";
            m_running = false;
        }

        public void shutdown()
        {
            abort();
        }

        public void abort()
        {
            if (m_running)
            {
                m_running = false;
                m_sendconnection.abort();
                m_receiveconnection.abort();
                //m_thread.Abort();
                m_thread.Join();
            }
        }

        public void start(int in_port)
        {
            if (!m_running)
            {
                IPAddress ipaddress = IPAddress.Parse("255.255.255.255");
                IPEndPoint ipendpoint = new IPEndPoint(ipaddress, in_port);
                UdpClient udpsendclient = new UdpClient();
                udpsendclient.EnableBroadcast = true;
                m_sendconnection = new udpconnection(udpsendclient, ipendpoint);

                UdpClient udpreceiveclient = new UdpClient(Port);
                udpreceiveclient.EnableBroadcast = true;
                IPEndPoint ipreceiverendpoint = new IPEndPoint(IPAddress.Any, 0);
                m_receiveconnection = new udpconnection(udpreceiveclient, ipreceiverendpoint);
                m_receiveconnection.start();
                m_receiveconnection.OnNetworkData += new udpconnection.OnNetworkDataDelegate(Connection_OnNetworkData);

                m_thread = new Thread(new ThreadStart(threadmain));
                m_thread.IsBackground = true;
                m_thread.Name = "detectionclient";                
                m_running = true;
                m_thread.Start();
            }
        }

        private void threadmain()
        {           
            while (m_running)
            {
                try
                {
                    messages.request_server_info message = new messages.request_server_info();
                    m_sendconnection.sendmessage(message);
                    Thread.Sleep(1000);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());    
                }
            }
        }

        void Connection_OnNetworkData(object sender, udpconnection.OnNetworkDataEvent e)
        {
            messages.message.context c = new messages.message.context();
            c.Connection = sender as connection;
            c.IPEndPoint = e.IPEndPoint;
            e.Message.process(c);
        } 
    }
}
